package com.ltmonitor.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ltmonitor.dao.IDriverInfoDao;
import com.ltmonitor.entity.DriverInfo;
import com.ltmonitor.service.IDriverInfoService;

@Service("driverInfoService")
public class DriverInfoService extends BaseService<Long, DriverInfo> implements IDriverInfoService {

	@Autowired
	private IDriverInfoDao DriverInfoDao;
	
	/**
	 * 获得驾驶员信息
	 * @param vehicleId
	 * @return
	 */
	@Override
	public DriverInfo getMainDriverInfo(long vehicleId)
	{
		String hsql = "from DriverInfo where vehicleId = ? and mainDriver = ? and deleted = ?";
		DriverInfo d =  this.find(hsql, new Object[]{vehicleId, true, false});
		return d;
	}
	

	@Override
	public void saveDriver(DriverInfo d)
	{
		if(d.isMainDriver() && d.getVehicleId() > 0)
		{
			String hsql = "from DriverInfo where vehicleId = ? and mainDriver = ? and entityId <> ? ";
			List ls = this.baseDao.query(hsql, new Object[]{d.getVehicleId(),true,d.getEntityId()});
			for(Object obj : ls)
			{
				DriverInfo dr = (DriverInfo)obj;
				dr.setMainDriver(false);
				this.baseDao.saveOrUpdate(dr);
			}
		}
		this.baseDao.saveOrUpdate(d);
	}
	

}
